const { Model, DataTypes } = require('sequelize');
const sequelize = require('../db/db');

class CommentLike extends Model { }
CommentLike.init(
  {
    id: {
      type: DataTypes.CHAR(18),
      primaryKey: true,
      allowNull: false,
      comment: '点赞ID'
    },
    comment_id: {
      type: DataTypes.CHAR(18),
      allowNull: false,
      comment: '评论ID'
    },
    user_id: {
      type: DataTypes.CHAR(18),
      allowNull: false,
      comment: '用户ID'
    },
    create_time: {
      type: DataTypes.DATE,
      defaultValue: DataTypes.NOW,
      comment: '创建时间'
    }
  },
  {
    sequelize,
    modelName: 'CommentLike',
    tableName: 'comment_like',
    timestamps: false,
    indexes: [
      {
        unique: true,
        fields: ['comment_id', 'user_id'],
        comment: '确保一个用户只能对一个评论点赞一次'
      }
    ]
  }
);

// 关联关系在模型定义后设置，避免循环依赖
const setupAssociations = () => {
  const User = require('./User');
  const QuestionComment = require('./QuestionComment');
  
  CommentLike.belongsTo(User, {
    foreignKey: 'user_id',
    targetKey: 'user_id',
    as: 'user'
  });

  CommentLike.belongsTo(QuestionComment, {
    foreignKey: 'comment_id',
    targetKey: 'id',
    as: 'comment'
  });
};

// 在其他模型加载完成后调用关联设置
setTimeout(setupAssociations, 0);

module.exports = CommentLike; 